<?php
/**
 * Copyright (c) 2016.  hangzhou BOYE .Co.Ltd. All rights reserved
 */

/**
 * Created by PhpStorm.
 * User: 1
 * Date: 2016-12-14
 * Time: 16:36
 */

namespace app\admin\controller;


use app\src\admin\helper\AdminSessionHelper;
use app\src\admin\helper\MenuHelper;
use app\src\deleteApply\logic\DeleteApplyLogic;
use app\src\insurance\logic\InsuranceApplyLogic;
use app\src\insurance\logic\InsuranceOrderLogic;
use app\src\order\logic\OrdersLogic;
use app\src\platformApply\logic\PlatformApplyLogic;
use app\src\seller\logic\SellerLogic;
use app\src\seller\logic\SellerOrderLogic;
use app\src\sellerApply\logic\SellerApplyLogic;
use app\src\withdrawDeposit\logic\WithdrawDepositLogic;
use think\Request;
use app\src\order\logic\OrdersInfoViewLogic;
use app\src\buyorder\logic\BuyOrderLogic;
use app\src\platform\logic\PlatformLogic;
use app\src\user\logic\VUserInfoLogic;

class Manager extends Admin
{

    //首页
    public function index(){
        return $this->boye_display();
    }

    //介绍
    public function about(){
        $data = [];
        //待处理平台申请数
        $result = (new PlatformApplyLogic)->count(['status' => 0, 'apply_status' => 0]);
        $data['platform_apply_count'] = $result['info'];

        //待处理商家申请数
        $result = (new SellerApplyLogic)->count(['status' => 0, 'apply_status' => 0]);
        $data['seller_apply_count'] = $result['info'];

        //待处理保险申请数
        $result = (new InsuranceApplyLogic)->count(['status' => 0, 'apply_status' => 0]);
        $data['insurance_apply_count'] = $result['info'];

        //待处理删除申请数
        $result = (new DeleteApplyLogic)->count(['status' => 0, 'apply_status' => 0]);
        $data['delete_apply_count'] = $result['info'];

        //今日订单数
        $start_time = strtotime('midnight this day');
        $end_time = strtotime('midnight next day');
        $result = (new SellerOrderLogic)->count(['status' => 0,'order_status' =>1, 'create_time' =>[['>=', $start_time], ['<', $end_time]]]);
        $data['today_seller_order_count'] = $result['info'];

        //昨日订单数
        $start_time = strtotime('midnight last day');
        $end_time = strtotime('midnight this day');
        $result = (new SellerOrderLogic)->count(['status' => 0,'order_status' =>1, 'create_time' =>[['>=', $start_time], ['<', $end_time]]]);
        $data['last_day_seller_order_count'] = $result['info'];

        //成交车险订单周一到周日
        $start_time = strtotime('midnight Monday this week');
        $end_time = strtotime('next day', $start_time);

        //提现申请数
        $result  = (new WithdrawDepositLogic())->count(['status' => 0,'apply_status'=>0]);
        $data['withdraw_deposit_count'] = $result['info'];
        $list = [];
        $result = (new InsuranceOrderLogic)->count(['status' => 0, 'create_time' =>[['>=', $start_time], ['<', $end_time]]]);
        $list[] = $result['info'];
        for($i = 0; $i < 6; $i++){
            $start_time = $end_time;
            $end_time = strtotime('next day', $start_time);
            $result = (new InsuranceOrderLogic)->count(['status' => 0, 'create_time' =>[['>=', $start_time], ['<', $end_time]]]);
            $list[] = $result['info'];
        }
        $data['week_insurance_order_count'] = json_encode($list);

        //商家订单1月到12月
        $list = [];
        for($i = 1; $i <= 12; $i++){
            $start_time = strtotime(date("Y-$i-01"));
            $end_time = strtotime('next month', $start_time);
            $result = (new SellerOrderLogic)->count(['status' => 0, 'create_time' =>[['>=', $start_time], ['<', $end_time]]]);
            $list[] = $result['info'];
        }
        $data['year_seller_order_count'] = json_encode($list);

        //保险统计
        $result = (new InsuranceOrderLogic)->count(['insurance_id' => 5, 'status' => 0]);
        $data['insurance_count'] = $result['info'];

        //最新商家7条
        $result = (new SellerLogic)->query(['status' => 0], ['curpage' => 1, 'size' => 7], 'id desc');
        $data['new_seller'] = $result['info']['list'];

        //最新平台7条
        $result = (new PlatformLogic)->query(['status' => 0], ['curpage' => 1, 'size' => 7], 'id desc');
        $data['new_platform'] = $result['info']['list'];

        $this->assign('data', $data);

        return $this->boye_display();
    }

    public function about_data(){
        $data = [];
        //待处理平台申请数
        $result = (new PlatformApplyLogic)->count(['status' => 0, 'apply_status' => 0]);
        $data['platform_apply_count'] = $result['info'];

        //待处理商家申请数
        $result = (new SellerApplyLogic)->count(['status' => 0, 'apply_status' => 0]);
        $data['seller_apply_count'] = $result['info'];

        //待处理保险申请数
        $result = (new InsuranceApplyLogic)->count(['status' => 0, 'apply_status' => 0]);
        $data['insurance_apply_count'] = $result['info'];

        //待处理删除申请数
        $result = (new DeleteApplyLogic)->count(['status' => 0, 'apply_status' => 0]);
        $data['delete_apply_count'] = $result['info'];

        //今日订单数
        $start_time = strtotime('midnight this day');
        $end_time = strtotime('midnight next day');
        $result = (new SellerOrderLogic)->count(['status' => 0,'order_status' =>1, 'create_time' =>[['>=', $start_time], ['<', $end_time]]]);
        $data['today_seller_order_count'] = $result['info'];

        //昨日订单数
        $start_time = strtotime('midnight last day');
        $end_time = strtotime('midnight this day');
        $result = (new SellerOrderLogic)->count(['status' => 0,'order_status' =>1, 'create_time' =>[['>=', $start_time], ['<', $end_time]]]);
        $data['last_day_seller_order_count'] = $result['info'];

        //成交车险订单周一到周日
        $start_time = strtotime('midnight Monday this week');
        $end_time = strtotime('next day', $start_time);

        //提现申请数
        $result  = (new WithdrawDepositLogic())->count(['status' => 0,'apply_status'=>0]);
        $data['withdraw_deposit_count'] = $result['info'];
        $list = [];
        $result = (new InsuranceOrderLogic)->count(['status' => 0, 'create_time' =>[['>=', $start_time], ['<', $end_time]]]);
        $list[] = $result['info'];
        for($i = 0; $i < 6; $i++){
            $start_time = $end_time;
            $end_time = strtotime('next day', $start_time);
            $result = (new InsuranceOrderLogic)->count(['status' => 0, 'create_time' =>[['>=', $start_time], ['<', $end_time]]]);
            $list[] = $result['info'];
        }
        $data['week_insurance_order_count'] = json_encode($list);

        //商家订单1月到12月
        $list = [];
        for($i = 1; $i <= 12; $i++){
            $start_time = strtotime(date("Y-$i-01"));
            $end_time = strtotime('next month', $start_time);
            $result = (new SellerOrderLogic)->count(['status' => 0, 'create_time' =>[['>=', $start_time], ['<', $end_time]]]);
            $list[] = $result['info'];
        }
        $data['year_seller_order_count'] = json_encode($list);

        //保险统计
        $result = (new InsuranceOrderLogic)->count(['insurance_id' => 5, 'status' => 0]);
        $data['insurance_count'] = $result['info'];

        //最新商家7条
        $result = (new SellerLogic)->query(['status' => 0], ['curpage' => 1, 'size' => 7], 'id desc');
        $data['new_seller'] = $result['info']['list'];

        //最新平台7条
        $result = (new PlatformLogic)->query(['status' => 0], ['curpage' => 1, 'size' => 7], 'id desc');
        $data['new_platform'] = $result['info']['list'];

            $this->success('获取数据成功', null, $data);


    }

    //
    public function well(){
        $this->redirect('manager/about');
        $url = request()->scheme().'://'.request()->host();
        $this->assign('admin_url',$url);

        return $this->boye_display();
    }

    public function userMenu()
    {
        if(IS_AJAX){
            $menuList = MenuHelper::getVMenu(UID);
            $this->success('获取用户菜单成功', null, $menuList);
        }
    }
    public function userData()
    {
        if(IS_AJAX){
            $menuList = MenuHelper::getVMenu(UID);
            $user_info = AdminSessionHelper::getUserInfo();

            $UCENTER_PLATFORM = config('UCENTER_PLATFORM');
            $user_info = [
                'id' => $user_info['id'],
                'username' => $user_info['username'],
                'nickname' => $user_info['nickname'],
                'head' => $user_info['head']
            ];
            $data = [
                'menuList' => $menuList,
                'userInfo' => $user_info,
                'platformInfo' => $UCENTER_PLATFORM
            ];
            $this->success('获取用户数据成功', null, $data);
        }
    }
}